﻿using Commons.Requests;
using Commons.ReturnData;
using OpenAI.Domain.Entities;
using OpenAI.Domain.IRepositories;
using OpenAI.Infrastructure.Requests;

namespace OpenAI.Infrastructure.Services
{
    public class ChatSessionService
    {
        public readonly IChatSessionRepository repository;

        public ChatSessionService(IChatSessionRepository repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 删除聊天
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<MessageResult> DeletedChatSessionAsync(Guid appUserId,int id)
        {
            var res = await repository.GetChatSessionsByUserIdAsync(appUserId);
            if(res.Total == 0 || res.Datas.Where(x=>x.AppUserId == appUserId).Count() == 0)
            {
                return new MessageResult(204, "删除失败没有当前聊天!", false);
            }
            else
            {

                return await repository.DeletedChatSessionAsync(id);
            }
            
        }
        /// <summary>
        /// 查找某一条聊天
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public async Task<ChatSession> GetChatSessionsByIdAsync(int id) => await repository.GetChatSessionsByIdAsync(id);

        /// <summary>
        /// 获取用户聊天列表
        /// </summary>
        /// <param name="paging"></param>
        /// <returns></returns>
        public async Task<DataResult<ChatSession>> GetChatSessionsByUserIdAsync(Guid appUserId, PagingRequest paging)
        {
            if (string.IsNullOrEmpty(paging.ExtendedValue1))
            {
                return await repository.GetChatSessionsByUserIdAsync(paging.PageIndex, paging.PageSize, x=>x.AppUserId == appUserId);

            }
            else
            {
                return await repository.GetChatSessionsByUserIdAsync(paging.PageIndex, paging.PageSize, x => x.Title.Contains(paging.ExtendedValue1)&& x.AppUserId == appUserId);
            }
        }
        /// <summary>
        /// 用户创建一条聊天
        /// </summary>
        /// <param name="chatSessionRequest"></param>
        /// <returns></returns>
        public async Task<MessageResult> CreateChatSessionAsync(ChatSessionRequest chatSessionRequest)
        {
            return await repository.CreateChatSessionAsync(chatSessionRequest.AppUserId, chatSessionRequest.Title);
        }
        /// <summary>
        /// 更改聊天标题
        /// </summary>
        /// <param name="chatSessionRequest"></param>
        /// <returns></returns>
        public async Task<MessageResult> UpdateChatSessionAsync(ChatSessionRequest chatSessionRequest)
        {
            var chat = await repository.GetChatSessionsByIdAsync(chatSessionRequest.Id);
            if(chat != null)
            {
                chat.Title = chatSessionRequest.Title;
                return await repository.UpdateChatSessionAsync(chat);
            }
            else
            {
                return new MessageResult(204,"当前聊天不存在",false);
            }
        }
        
    }
}
